package top.hymsk.ecommerce.pojo.response;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import top.hymsk.ecommerce.pojo.status.ResponseStatusCode;

/**
 * 通用响应类
 *
 * @author 花于陌上开
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GeneralResponse {
    Integer code;
    String msg;
    Object data;

    public GeneralResponse(ResponseStatusCode responseStatusCode, Object data) {
        this.code = responseStatusCode.getStatusCode();
        this.msg = responseStatusCode.getDescription();
        this.data = data;
    }

    /**
     * 获取成功响应对象
     *
     * @return 响应对象
     */
    public static GeneralResponse success() {
        return new GeneralResponse(ResponseStatusCode.SUCCESSFUL, null);
    }

    /**
     * 获取成功响应对象，携带对应数据
     *
     * @param data 携带响应数据
     * @return 响应对象
     */
    public static GeneralResponse success(Object data) {
        return new GeneralResponse(ResponseStatusCode.SUCCESSFUL, data);
    }

    /**
     * 返回失败响应
     *
     * @param responseStatusCode 状态码
     * @return 响应对象
     */
    public static GeneralResponse fail(ResponseStatusCode responseStatusCode) {
        return new GeneralResponse(responseStatusCode, null);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
